/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Negocios;

import CBD.FabricaDeSesiones;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author Dany
 */
public class ControlCitas {
     private Session session;
    private Transaction transaction;
    private SessionFactory sessionFactory;

    public ControlCitas() {
    }

    public List select(String HQL) {
        List listaObjetos;
        try {
            iniciaSF();
            listaObjetos = session.createQuery(HQL).list();
            return listaObjetos;
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }
        return null;
    }

    public void add(Object objeto) {
        try {
            iniciaSF();
            session.save(objeto);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }
    }

    public void Update(Object objeto) {
        try {
            iniciaSF();
            session.update(objeto);
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }
    }

    public void delete(Object objeto) {
        try {
            iniciaSF();
            session.delete(objeto);
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }

    }

    public void iniciaSF() {
        try {
            session = null;
            sessionFactory = FabricaDeSesiones.getSessionFactory();
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e + "\n"
                    + "En el controlador de producto.", "Error al iniciar Session",
                    0);
        }

    }

    public void cierraSF() {
        try {
            transaction.commit();
            session.flush();
            session.close();
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e + "\n"
                    + "En el controlador de producto.", "Error al cerrar Session",
                    0);
        }
    }
}
